// MongoDB初始化脚本
// 在MongoDB shell中执行: load('mongodb-init.js')

// 创建数据库
use production_tracker;

// 创建集合
db.createCollection("work_plans");
db.createCollection("materials");
db.createCollection("work_order_data");
db.createCollection("stencils");
db.createCollection("fixtures");
db.createCollection("sop_approvals");

// 创建索引
db.work_plans.createIndex({ "model_name": 1 });
db.work_plans.createIndex({ "status": 1 });
db.materials.createIndex({ "model": 1 });
db.stencils.createIndex({ "model": 1 });
db.stencils.createIndex({ "status": 1 });
db.fixtures.createIndex({ "model": 1 });
db.fixtures.createIndex({ "status": 1 });
db.sop_approvals.createIndex({ "model": 1 });
db.sop_approvals.createIndex({ "status": 1 });

// 插入示例数据
db.work_plans.insertMany([
    {
        model_name: "Model-A",
        work_order: "WO-2024-001",
        quantity: 100,
        date: "2024-01-15",
        status: "pending",
        notes: "紧急订单",
        created_at: new Date(),
        updated_at: new Date()
    },
    {
        model_name: "Model-B",
        work_order: "WO-2024-002",
        quantity: 200,
        date: "2024-01-20",
        status: "in-progress",
        notes: "正常生产",
        created_at: new Date(),
        updated_at: new Date()
    },
    {
        model_name: "Model-C",
        work_order: "WO-2024-003",
        quantity: 150,
        date: "2024-01-10",
        status: "delayed",
        notes: "物料延迟",
        created_at: new Date(),
        updated_at: new Date()
    }
]);

db.materials.insertMany([
    {
        serial: 1,
        model: "Model-A",
        name: "电阻",
        quantity: 1000,
        location: "仓库A",
        notes: "标准件",
        created_at: new Date(),
        updated_at: new Date()
    },
    {
        serial: 2,
        model: "Model-B",
        name: "电容",
        quantity: 2000,
        location: "仓库B",
        notes: "高容量",
        created_at: new Date(),
        updated_at: new Date()
    }
]);

db.work_order_data.insertMany([
    {
        work_order: "WO-2024-001",
        work_order_type: "生产工单",
        part_number: "PN-001",
        part_name: "主板",
        specification: "标准",
        category: "电子",
        work_order_quantity: 100,
        version: "V1.0",
        production_date: "2024-01-15",
        process: "SMT",
        stencil_top: true,
        stencil_bot: false,
        reflow_fixture: true,
        wave_fixture: false,
        press_fixture: true,
        bom: true,
        sop: true,
        production_materials: true,
        type: "标准",
        created_at: new Date(),
        updated_at: new Date()
    }
]);

db.stencils.insertMany([
    {
        serial: 1,
        model: "Model-A",
        quantity: 100,
        open_time: "2024-01-05",
        acceptance_time: "2024-01-10",
        status: "completed",
        notes: "已验收",
        created_at: new Date(),
        updated_at: new Date()
    },
    {
        serial: 2,
        model: "Model-B",
        quantity: 200,
        open_time: "2024-01-08",
        acceptance_time: "2024-01-12",
        status: "in-progress",
        notes: "验收中",
        created_at: new Date(),
        updated_at: new Date()
    }
]);

db.fixtures.insertMany([
    {
        serial: 1,
        model: "Model-A",
        quantity: 50,
        open_time: "2024-01-06",
        acceptance_time: "2024-01-11",
        status: "completed",
        notes: "已入库",
        created_at: new Date(),
        updated_at: new Date()
    },
    {
        serial: 2,
        model: "Model-B",
        quantity: 100,
        open_time: "2024-01-09",
        acceptance_time: "2024-01-13",
        status: "pending",
        notes: "待验收",
        created_at: new Date(),
        updated_at: new Date()
    }
]);

db.sop_approvals.insertMany([
    {
        serial: 1,
        model: "Model-A",
        status: "approved",
        submit_time: "2024-01-05",
        approval_time: "2024-01-08",
        notes: "已通过",
        created_at: new Date(),
        updated_at: new Date()
    },
    {
        serial: 2,
        model: "Model-B",
        status: "pending",
        submit_time: "2024-01-07",
        approval_time: null,
        notes: "待签核",
        created_at: new Date(),
        updated_at: new Date()
    }
]);

print("MongoDB初始化完成！");
